import 'server-only' //代表仅服务端可用
import Joi from 'joi';
import resFn from '@/app/api/util/res'
import { NextRequest } from 'next/server';
import { logError } from '@/app/api/util/io/log';
import { getBody, judge, permissionJudge } from '@/app/api/util/res/handle';
import { IsUploadedReq, IsUploadedRes } from '@/types/api/file/sliceUpload';
import { stringJoi } from '@/app/api/util/Joi';
import { fileExists } from '@/app/api/util/io';
import { baseGetPath } from '@/app/(dashboard)/common/config/baseURL';
import { getAbsPath } from '@/app/api/util/other';
import { getCommonPath, getCommonPathByOwnFileName, getPublicPath, getServerPath, getTempDir } from '../../getPath';
export const dynamic = 'force-dynamic';

/**文件是否上传过，实现秒传、断点续传 接口*/
export async function POST(request: NextRequest) {
    try {
        const user_id = "JZ8740"
        const { md5, path, suffix, ownFileName } = await judge(await getBody<IsUploadedReq>(request), { md5: stringJoi(), path: stringJoi(), suffix: stringJoi(), ownFileName: stringJoi({ required: false }) })
        // const commonPath = getCommonPath(user_id, md5, suffix, path)
        const commonPath = ownFileName ? getCommonPathByOwnFileName(user_id, md5, ownFileName, suffix, path) : getCommonPath(user_id, md5, suffix, path)
        const targetFilePath = getServerPath(commonPath)
        /**是否完整的上传过 */
        const flag = fileExists(targetFilePath)
        const noComplete = fileExists(getTempDir(user_id, md5))
        return resFn.success<IsUploadedRes>({
            flag: flag,
            path: flag ? getPublicPath(commonPath) : "",
            noComplete
        });
    } catch (error: any) {
        logError('文件是否上传过，实现秒传、断点续传 接口', error)
        return resFn.errorWithReject(error);
    }
}